/*
  ******************************************************************************
  *
  * COPYRIGHT(c) 2020, China Mobile IOT
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of China Mobile IOT nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */ 

/**
 * @file delay.c
 * @author CMIOT
 * @version v1.0.0
 *
 * @COPYRIGHT(c) 2020, China Mobile IOT. All rights reserved.
 */
#include "delay.h"

static u8 us_base  = 0; // Number of systick clocks required per us.
static u16 ms_base = 0; // Number of systick clocks required per ms.

/**
 * @brief  Initialization delay function.
 */
void delay_init(void)
{
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); // Select external clock,HCLK/8.
    us_base = SystemCoreClock / 8000000;                  // Number of systick clocks required per us.
    ms_base = (u16)us_base * 1000;                        // Number of systick clocks required per ms.
}

/**
 * @brief  Function of delay nus.
 * @param  u32 nus:Delay number of us.
 */
void delay_us(u32 nus)
{
    u32 temp;
    SysTick->LOAD = nus * us_base;            // load the time
    SysTick->VAL  = 0x00;                     // clear the counter
    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // start to countdown
    do
    {
        temp = SysTick->CTRL;
    } while ((temp & 0x01) && !(temp & (1 << 16))); // waiting time arrives
    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;      // turn off the counter
    SysTick->VAL = 0X00;                            // clear the counter
}
/**
 * @brief  Function of delay nms.Note the range of nms,less than or equal to 0xffffff*8*1000/SYSCLK.While SYSCLK is
 * equal to 72MHz,nms<=1864.
 * @param  u32 nms:Delay number of ms.
 */
void delay_ms(u16 nms)
{
    u32 temp;
    SysTick->LOAD = (u32)nms * ms_base;       // load the time
    SysTick->VAL  = 0x00;                     // clear counter
    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // start to countdown
    do
    {
        temp = SysTick->CTRL;
    } while ((temp & 0x01) && !(temp & (1 << 16))); // waiting time arrives
    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;      // turn off the counter
    SysTick->VAL = 0X00;                            // clear the counter
}
